package controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojos.common.ReplyMessage;
import pojos.parents.ParentsInfo;
import service.ParentsInfoService;
import utils.InputVerifyUtils;

import javax.annotation.Resource;

/**
 * 管理员家长管理控制器
 */
@Controller
@RequestMapping("/admin")
public class AdminForParentsController {

    @Resource(name = "parentsInfoService")
    private ParentsInfoService parentsInfoService;

    /**
     * 添加家长信息
     */
    @PostMapping("/frame/addParents-submit")
    @ResponseBody
    public ReplyMessage addParents(@RequestParam(value = "loginName",required = false) String loginName,
                                   @RequestParam(value = "loginPswd",required = false) String loginPswd,
                                   @RequestParam(value = "phone", required = false) String phoneNum,
                                   @RequestParam(value = "email", required = false) String emailNum,
                                   @RequestParam(value = "userName",required = false) String userName,
                                   @RequestParam(value = "sex",required = false) Integer userSex,
                                   @RequestParam(value = "QQ",required = false) String QQ,
                                   @RequestParam(value = "wechat",required = false) String weChat,
                                   @RequestParam(value = "child",required = false) Integer child,
                                   @RequestParam(value = "comment",required = false) String comment){
        ReplyMessage replyMessage = new ReplyMessage();
        ParentsInfo parentsInfo = new ParentsInfo();

        // 登录名验证
        if(loginName == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录名称不能为空！");
            return replyMessage;
        }
        int loginNameLen = loginName.length();
        if(loginNameLen < 2 || loginNameLen > 20){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录名长度不正确！");
            return replyMessage;
        }

        // 登录密码验证
        if(loginPswd == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录密码不能为空！");
            return replyMessage;
        }
        if(loginPswd.length() != 32){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录密码不正确！");
            return replyMessage;
        }

        // 手机号验证
        if(phoneNum != null && phoneNum.length() > 0){
            if(InputVerifyUtils.isPhoneNotRight(phoneNum)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("手机号不正确！");
                return replyMessage;
            }
            parentsInfo.setPhone(phoneNum);
        }

        // 邮箱验证
        if(emailNum != null && emailNum.length() > 0){
            if(InputVerifyUtils.isEmailNotRight(emailNum)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("邮箱号不正确！");
                return replyMessage;
            }
            parentsInfo.setEmail(emailNum);
        }

        // 真实姓名验证
        if(userName != null && userName.length() > 0){
            if(InputVerifyUtils.isRealNameNotRight(userName)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("用户真实姓名不正确！");
                return replyMessage;
            }
            parentsInfo.setRealName(userName);
        }

        // 性别判断
        if(userSex != null){
            if(userSex == 1){
                parentsInfo.setSex((byte) 1);
            }else{
                parentsInfo.setSex((byte) 0);
            }
        }

        // QQ号判断
        if(QQ != null && QQ.length() > 0){
            if(InputVerifyUtils.isQQNotRight(QQ)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("QQ号不正确！");
                return replyMessage;
            }
            parentsInfo.setQQ(QQ);
        }

        // 微信号判断
        if(weChat != null && weChat.length() > 0){
            if(InputVerifyUtils.isWeChatNotRight(weChat)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("微信号不正确");
                return replyMessage;
            }
            parentsInfo.setWeChat(weChat);
        }

        // 子女ID
        if(child != null){
            parentsInfo.setChild(child);
        }

        // 备注验证
        if(comment != null){
            if(comment.length() > 85){
                replyMessage.setStatus(400);
                replyMessage.setInfo("备注不允许超过85个字符！");
                return replyMessage;
            }
            parentsInfo.setComment(comment);
        }

        parentsInfo.setName(loginName);
        parentsInfo.setPassword(loginPswd);
        return parentsInfoService.addParentsInfo(parentsInfo);
    }

    @PostMapping("/frame/queryAllParentsName")
    @ResponseBody
    public ReplyMessage queryAllParentsName(){
        return parentsInfoService.queryAllParentsName();
    }
}
